define(["angularAMD"], function (angularAMD) {
    angularAMD.service('E01badreasonService', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog){
            var _service = this;
            _service.GetReasonList = function(data, callback){
                var query_condition_info = [];
                query_condition_info.push({column_name:'ReasonSubType', query_mode:'0', value:'Scrap phenomenon', merge_condition_model:'AND' });
                query_condition_info.push({column_name:'OPNo', query_mode:'0', value:data.op_no, merge_condition_model:'AND' });
                query_condition_info.push({column_name:'ReasonNo', query_mode:'0', value:data.reason_no, merge_condition_model:'AND' });
                $MMWService.pageNumberInfoGet({
                    page_mode : '15',
                    query_condition_info: query_condition_info
                }, function(items){
                    if(typeof callback === 'function')
                        callback(items);
                });
            }
            _service.init = function(options){
                $mdDialog.dialog(
                    'module/moduleE/program/E01/badreason.tmp.html',
                    function(dialog){
                        return {
                            css_url:'module/moduleE/program/E01/badreason.css',
                            itemList:angular.copy(options.itemList),
                            data:{
                                reason_no:'',
                                reason_name:''
                            },
                            beforeShown: function(){

                            },
                            loadReason: function(callback){
                                $MMWService.showQueryListPNIG({
                                    page_mode: 15,
                                    condition: [
                                        {name:$filter('translate')('common.queryList.badReason_c'), column_name:'ReasonNo', query_mode:'2', value:dialog.data.reason_no}
                                    ],
                                    header: [
                                        {label:$filter('translate')('common.queryList.badReason_c'), code:'page_no'},
                                        {label:$filter('translate')('common.queryList.badReason_n'), code:'page_name'}
                                    ],
                                    setCondition:function(conditions){
                                        conditions.push({column_name:'ReasonSubType', query_mode:'0', value:'Scrap phenomenon', merge_condition_model:'AND' });
                                        conditions.push({column_name:'OPNo', query_mode:'0', value:options.op_no, merge_condition_model:'AND' });
                                        return conditions;
                                    },
                                    selectCode: dialog.data.reason_no,
                                    confirm : function(selectItem){
                                        dialog.data.reason_no = selectItem.page_no;
                                        dialog.data.reason_name = selectItem.page_name;
                                        if(typeof(callback) == 'function'){
                                            callback();
                                        }
                                    }
                                });
                            },
                            addReason: function(event){
                                if(event ==undefined || event.keyCode == 13){
                                    if(!dialog.data.reason_no || dialog.data.reason_no == ''){
                                        //請輸入原因編號
                                        //$rootScope.showAlert($filter('translate')('E07.msg.reason_empty'));
                                        dialog.loadReason(dialog.addReason);
                                    } else {
                                        //重複檢核
                                        for (var i = 0; i < dialog.itemList.length; i++){
                                            //原因重複
                                            if(dialog.itemList[i].reason_no == dialog.data.reason_no){
                                                $rootScope.showAlert(dialog.data.reason_no + $filter('translate')('E07.msg.reason_duplicate'));
                                                return;
                                            }
                                        }
                                        if(dialog.data.reason_name == ''){
                                            _service.GetReasonList({
                                                reason_no: dialog.data.reason_no,
                                                op_no: options.op_no
                                            }, function(items){
                                                if(items.length > 0){
                                                    dialog.itemList.push({
                                                        reason_no: items[0].page_no,
                                                        reason_name: items[0].page_name
                                                    });
                                                    dialog.data = {
                                                        reason_no:'',
                                                        reason_name:''
                                                    };
                                                } else {
                                                    $rootScope.showAlert($filter('translate')('E07.msg.reason_not_exist'));
                                                }
                                            });
                                        } else {
                                            dialog.itemList.push({
                                                reason_no: dialog.data.reason_no,
                                                reason_name: dialog.data.reason_name
                                            });
                                            dialog.data = {
                                                reason_no:'',
                                                reason_name:''
                                            };
                                        }
                                    }
                                }
                            },
                            removeReason: function(item){
                                dialog.itemList.splice(dialog.itemList.indexOf(item), 1);
                            },
                            back: function () {
                                dialog.hide();
                                if(options.close)
                                    options.close();
                            },
                            confirm: function () {
                                if(typeof(options.callback) == "function")
                                    options.callback(dialog);
                            }
                        };
                    }
                );
            }
        }
    ]);
});
